package com.office.automate.email.framework;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.office.automate.configuration.property.ConfigurationProperties;

public class HandlingEmails {

	private static final Properties configProps = ConfigurationProperties.getProps();

	public void sendEmailTo(String aRecipient, String aSubject, String aMessageBody) {
	try {
		
		MimeMessage message = new MimeMessage(EmailConfigurations.getSession());
		message.setFrom(new InternetAddress(configProps.getProperty("MAIL_ACCOUNT")));
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(aRecipient));
		message.setSubject(aSubject);
		message.setText(aMessageBody);
		Transport.send(message);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	public void sendEmailTo(String[] aRecipients, String aSubject, String aMessageBody) {
		try {
			if (aRecipients != null && aRecipients.length > 0) {
				InternetAddress[] internetAddressList = new InternetAddress[aRecipients.length];
				for (int i=0; i < aRecipients.length; i++) {
					internetAddressList[i] = new InternetAddress(aRecipients[i]);
				}
				MimeMessage message = new MimeMessage(EmailConfigurations.getSession());
				message.setFrom(new InternetAddress(configProps.getProperty("MAIL_ACCOUNT")));
				message.setRecipients(Message.RecipientType.TO, internetAddressList);
				message.setSubject(aSubject);
				message.setText(aMessageBody);
				Transport.send(message);
			}
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
}